/*
 * @Descripttion: 字符串相关的操作
 * @version:
 * @Author: afeng510
 * @Date: 2021-03-18 21:48:34
 * @LastEditors: afeng510
 * @LastEditTime: 2023-01-12 17:00:59
 */
// 判断是否是图片文件
export const isImgFile = (img: string) => {
  const imgStr = "jpg,png,jpeg";
  var picNameStr = img.substring(img.lastIndexOf(".") + 1);
  if (imgStr.indexOf(picNameStr.toLowerCase()) > -1) {
    return true;
  }
  return false;
};
// 手机号验证
export const validPhone = (phone: string) => {
  const reg = /^1[3456789]\d{9}$/;
  return reg.test(phone);
};
// null判断
export const isObjNull = (obj: any) => {
  return !obj && typeof obj !== "undefined" && obj !== 0;
};
// undefined
export const isObjUndefined = (obj: any) => {
  return !obj && typeof obj !== "undefined";
};
// 随机字符串
export const randomString = (len: number) => {
  len = len || 32;
  var $chars =
    "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  var maxPos = $chars.length;
  var pwd = "";
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
};
// 去除字符串中的特殊字符
export const formatSpecialCharactersString = (val: string) => {
  return val.replace(/[\'\"\\\/\b\f\n\r\t]/g, "");
};
// 根据身份证号获取性别
export const returnGenderByIdCard = (idCardNumber: string) => {
  if (!idCardNumber || idCardNumber === "") {
    return "未知";
  }
  const lastDigit = parseInt(idCardNumber.charAt(idCardNumber.length - 2));
  if (lastDigit % 2 === 1) {
    return "男性";
  }
  if (lastDigit % 2 === 0) {
    return "女性";
  }
  return "未知";
};
// 根据身份证号获取 生日
export const returnBirthDayByIdCard = (idCardNumber: string) => {
  if (!idCardNumber || idCardNumber === "") {
    return "/";
  }
  const birthYear = parseInt(idCardNumber.substring(6, 10));
  const birthMonth = parseInt(idCardNumber.substring(10, 12));
  const birthDay = parseInt(idCardNumber.substring(12, 14));
  return `${birthMonth}月${birthDay}日`;
};
// 根据身份证号获取 年龄
export const returnAgeByIdCard = (idCardNumber: string) => {
  if (!idCardNumber || idCardNumber === "") {
    return "/";
  }
  const birthYear = parseInt(idCardNumber.substring(6, 10));
  const birthMonth = parseInt(idCardNumber.substring(10, 12));
  const birthDay = parseInt(idCardNumber.substring(12, 14));
  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1;
  var currentDay = currentDate.getDate();

  var age = currentYear - birthYear;
  if (
    currentMonth < birthMonth ||
    (currentMonth == birthMonth && currentDay < birthDay)
  ) {
    age--;
  }
  return age+'岁';
};
